home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the 3D Game Programming Gurus / gurus.iso / DirectX / dx9sdkcp.exe / SDK (C++) / Samples / Media / LightingVS.fx < prev    next >
Encoding:
Text File  |  2002-11-12  |  11.4 KB  |  336 lines

  1. //
  2. // Shaders used by the LightingVS D3D sample 
  3. //
  4. // Note: This effect file does not work with EffectEdit.
  5. //
  6.  
  7. // This shader transforms input vertices to the projection space and
  8. // computes lighting in the camera space for different light types.
  9. // Specular color is not computed, because the sample disables specular lighting.
  10. //
  11. // Constant allocation
  12. //
  13. // c0   - material ambient color 
  14. // c1   - material emissive color 
  15. // c2   - material diffuse color 
  16. // c3   - material specular color 
  17. // c4.x - material specular power 
  18. //
  19. // Each light is described by 7 vectors:
  20. //
  21. // c5  - light ambient color multiplied by material ambient color
  22. // c6  - light diffuse color 
  23. // c7  - light specular color 
  24. // c8  - light direction in camera space (normalized vector)
  25. // c9  - light position in camera space  
  26. // c10 - light attenuation factors (att0, att1, att2, lightType) 
  27. // c11 - light parameters (falloff, -cos(phi/2), 1/(cos(theta/2) - cos(phi/2)), 0) 
  28. // 
  29. // c12 -  2 light 
  30. // c19 -  3 light 
  31. // c26 -  4 light 
  32. // c33 -  5 light 
  33. // c40 -  6 light 
  34. // c47 -  7 light 
  35. // c54 -  8 light 
  36. // c61 -  9 light 
  37. // c67 - 10 light 
  38. // 
  39. // c100 - c103   - transformation matrix to the projection space 
  40. // c104 - c107   - transformation matrix to the camera space 
  41. // c108 - c111   - inverse transposed of transformation matrix to the camera space 
  42. // 
  43. // c112  - (0,1,2,3) 
  44. // c113  - initial diffuse color (emissive + material_ambient * global_ambient) 
  45. // 
  46. // i0            - (number of directional lights, StartIndex, 7, 0) 
  47. // i1            - (number of spot lights, StartIndex, 7, 0) 
  48. // i2            - (number of point lights, StartIndex, 7, 0) 
  49. // 
  50.  
  51. #define MaterialAmbient                c0 
  52. #define MaterialEmissive               c1 
  53. #define MaterialDiffuse                c2 
  54. #define MaterialSpecular               c3  
  55. #define MaterialSpecularPower          c4.x 
  56.  
  57. #define GlobalAmbientFactor            c113 
  58.  
  59. #define Mproj                          c100 
  60. #define Mcamera                        c104 
  61. #define Mcamera_inverse_transposed     c108 
  62.  
  63. #define VertexPosition                 v0 
  64. #define VertexNormal                   v1 
  65.  
  66. #define LightAmbient                   c5[aL] 
  67. #define LightDiffuse                   c6[aL] 
  68. #define LightSpecular                  c7[aL] 
  69. #define LightDirection                 c8[aL]
  70. #define LightPosition                  c9[aL] 
  71. #define LightAttenuation0              c10[aL].x 
  72. #define LightAttenuation1              c10[aL].y 
  73. #define LightAttenuation2              c10[aL].z 
  74. #define LightFallof                    c11[aL].x 
  75. #define LightSpotFactor1               c11[aL].y 
  76. #define LightSpotFactor2               c11[aL].z 
  77.  
  78. #define Zero                           c112.x 
  79. #define One                            c112.y 
  80.  
  81. #define VertexPositionCameraSpace      r0.xyz
  82. #define VertexNormalCameraSpace        r1.xyz
  83. #define OutputDiffuse                  r2 
  84. #define TempColor                      r4   
  85. #define LightToVertexVector            r5 
  86. #define AttenuationFactor              r6.x 
  87. #define SpotFactor                     r6.y 
  88. #define Rho                            r6.y 
  89. #define LightVertexDistance            r6.z 
  90. #define DotProduct                     r7.x 
  91.  
  92. VertexShader Lighting_VS_2_0 = asm 
  93.     vs_2_0 
  94.  
  95.     ; input declaration 
  96.     dcl_position0 VertexPosition 
  97.     dcl_normal0   VertexNormal 
  98.  
  99.     ;transform position to the projection space 
  100.     m4x4 oPos, VertexPosition, Mproj
  101.  
  102.     ;initialize output diffuse color
  103.     mov OutputDiffuse, GlobalAmbientFactor
  104.  
  105.     ;transform position to the camera space 
  106.     m4x3 VertexPositionCameraSpace, VertexPosition, Mcamera
  107.  
  108.     ;transform normal to the camera space 
  109.     ;assume that the transformation matrix is orthogonal, so we do not normalize 
  110.     ;normals after transformation
  111.     m3x3 VertexNormalCameraSpace, VertexNormal, Mcamera_inverse_transposed
  112.  
  113.     // Loop through directional lights 
  114.  
  115.     loop aL, i0 
  116.         mov LightToVertexVector, LightDirection     // Vector from light to vertex
  117.         mov AttenuationFactor, One                  // att * spot = 1.0f
  118.         call l0 
  119.     endloop 
  120.  
  121.     // Loop through spot lights 
  122.  
  123.     loop aL, i1 
  124.         ;compute distance to the light  and  
  125.         ;normalized vector from light to a vertex  
  126.         add LightToVertexVector, VertexPositionCameraSpace, -LightPosition 
  127.         dp3 LightVertexDistance, LightToVertexVector, LightToVertexVector
  128.         rsq LightVertexDistance, LightVertexDistance       
  129.         mul LightToVertexVector, LightToVertexVector, LightVertexDistance   
  130.         rcp LightVertexDistance, LightVertexDistance 
  131.  
  132.         ; compute attenuation factor 
  133.         mad AttenuationFactor, LightAttenuation2, LightVertexDistance, LightAttenuation1 
  134.         mad AttenuationFactor, AttenuationFactor, LightVertexDistance, LightAttenuation0 
  135.         rcp AttenuationFactor, AttenuationFactor 
  136.  
  137.         ; compute spot factor 
  138.         dp3 Rho, LightToVertexVector, LightDirection 
  139.         add SpotFactor, Rho, LightSpotFactor1              ; Rho - cos(phi/2) 
  140.         max SpotFactor, SpotFactor, Zero                   ; max((Rho - cos(phi/2), 0) 
  141.         mul SpotFactor, SpotFactor, LightSpotFactor2 
  142.         pow SpotFactor, SpotFactor, LightFallof      
  143.  
  144.         ;combined att*spot 
  145.         mul AttenuationFactor, AttenuationFactor, SpotFactor 
  146.         call l0 
  147.     endloop 
  148.  
  149.     // Loop through point lights 
  150.  
  151.     loop aL, i2 
  152.         ;compute distance to the light and a normalized vector from light to a vertex 
  153.         add LightToVertexVector, VertexPositionCameraSpace, -LightPosition  
  154.         dp3 LightVertexDistance, LightToVertexVector, LightToVertexVector
  155.         rsq LightVertexDistance, LightVertexDistance 
  156.         mul LightToVertexVector, LightToVertexVector, LightVertexDistance   
  157.         rcp LightVertexDistance, LightVertexDistance 
  158.  
  159.         ; compute attenuation factor 
  160.         mad AttenuationFactor, LightAttenuation2, LightVertexDistance, LightAttenuation1 
  161.         mad AttenuationFactor, AttenuationFactor, LightVertexDistance, LightAttenuation0 
  162.         rcp AttenuationFactor, AttenuationFactor 
  163.  
  164.         call l0 
  165.     endloop 
  166.  
  167.     ;output the result color 
  168.     mov oD0, OutputDiffuse 
  169.     mov oD0.w, MaterialDiffuse.w 
  170.     ret 
  171.  
  172.     // This function computes diffuse component and updates the result color
  173.     // Parameters:
  174.     //      Normal in camera space
  175.     //      Current diffuse color (in/out)
  176.     //      Vector from vertex to the light
  177.     //      Attenuation factor * spot factor
  178.     //
  179.     label l0 
  180.         dp3 DotProduct, VertexNormalCameraSpace, -LightToVertexVector
  181.         max DotProduct, DotProduct, Zero 
  182.         mul TempColor, DotProduct, LightDiffuse    
  183.         mul TempColor, TempColor, MaterialDiffuse  
  184.         add TempColor, TempColor, LightAmbient     
  185.         mad OutputDiffuse, TempColor, AttenuationFactor, OutputDiffuse 
  186.     ret
  187. };
  188.  
  189. VertexShader Lighting_VS_3_0 = asm
  190. {
  191.     vs_3_0 
  192.  
  193.     ; input declaration 
  194.     dcl_position0 VertexPosition 
  195.     dcl_normal0   VertexNormal 
  196.  
  197.     ;output declaration
  198.     dcl_position0 o0 
  199.     dcl_color0    o1 
  200.  
  201.  
  202.     ;transform position to the projection space 
  203.     m4x4 o0, VertexPosition, Mproj
  204.  
  205.     ;initialize output diffuse color
  206.     mov OutputDiffuse, GlobalAmbientFactor
  207.  
  208.     ;transform position to the camera space 
  209.     m4x3 VertexPositionCameraSpace, VertexPosition, Mcamera
  210.  
  211.     ;transform normal to the camera space 
  212.     ;assume that the transformation matrix is orthogonal, so we do not normalize 
  213.     ;normals after transformation
  214.     m3x3 VertexNormalCameraSpace, VertexNormal, Mcamera_inverse_transposed
  215.  
  216.     // Loop through directional lights 
  217.  
  218.     loop aL, i0 
  219.         mov LightToVertexVector, LightDirection     // Vector from light to vertex
  220.         mov AttenuationFactor, One                  // att * spot = 1.0f
  221.         call l0 
  222.     endloop 
  223.  
  224.     // Loop through spot lights 
  225.  
  226.     loop aL, i1 
  227.         add LightToVertexVector, VertexPositionCameraSpace, -LightPosition 
  228.         dp3 DotProduct, VertexNormalCameraSpace, -LightToVertexVector
  229.         if_gt DotProduct, Zero
  230.             ; compute spot factor 
  231.             dp3 Rho, LightToVertexVector, LightDirection 
  232.             if_gt Rho, Zero
  233.                 ;compute distance to the light  and  
  234.                 ;normalized vector from light to a vertex  
  235.                 dp3 LightVertexDistance, LightToVertexVector, LightToVertexVector
  236.                 rsq LightVertexDistance, LightVertexDistance       
  237.                 mul LightToVertexVector, LightToVertexVector, LightVertexDistance   
  238.                 ;rcp LightVertexDistance, LightVertexDistance 
  239.  
  240.                 ; compute attenuation factor 
  241.                 ;mad AttenuationFactor, LightAttenuation2, LightVertexDistance, LightAttenuation1 
  242.                 ;mad AttenuationFactor, AttenuationFactor, LightVertexDistance, LightAttenuation0 
  243.                 ;rcp AttenuationFactor, AttenuationFactor 
  244.                 mov AttenuationFactor, One
  245.  
  246.                 ; compute spot factor 
  247.                 dp3 Rho, LightToVertexVector, LightDirection 
  248.                 add SpotFactor, Rho, LightSpotFactor1              ; Rho - cos(phi/2) 
  249.                 if_gt Rho, Zero
  250.                     mul SpotFactor, SpotFactor, LightSpotFactor2 
  251.                     ; Use low precision method to compute pow(SpotFactor, LightFallof)
  252.                     logp TempColor.w, SpotFactor
  253.                     mul  TempColor.w, TempColor, LightFallof 
  254.                     expp SpotFactor, TempColor.w 
  255.  
  256.                     ;combined att*spot 
  257.                     mul AttenuationFactor, AttenuationFactor, SpotFactor 
  258.                     call l0 
  259.                 endif
  260.             endif
  261.         endif
  262.     endloop 
  263.  
  264.     // Loop through point lights 
  265.  
  266.     loop aL, i2 
  267.         ;compute distance to the light and a normalized vector from light to a vertex 
  268.         add LightToVertexVector, VertexPositionCameraSpace, -LightPosition  
  269.         dp3 LightVertexDistance, LightToVertexVector, LightToVertexVector
  270.         rsq LightVertexDistance, LightVertexDistance 
  271.         mul LightToVertexVector, LightToVertexVector, LightVertexDistance   
  272.         rcp LightVertexDistance, LightVertexDistance 
  273.  
  274.         ; compute attenuation factor 
  275.         mad AttenuationFactor, LightAttenuation2, LightVertexDistance, LightAttenuation1 
  276.         mad AttenuationFactor, AttenuationFactor, LightVertexDistance, LightAttenuation0 
  277.         rcp AttenuationFactor, AttenuationFactor 
  278.  
  279.         call l0 
  280.     endloop 
  281.  
  282.     ;output the result color 
  283.     mov o1, OutputDiffuse 
  284.     mov o1.w, MaterialDiffuse.w 
  285.     ret 
  286.  
  287.     // This function computes diffuse component and updates the result color
  288.     // Parameters:
  289.     //      Normal in camera space
  290.     //      Current diffuse color (in/out)
  291.     //      Vector from vertex to the light
  292.     //      Attenuation factor * spot factor
  293.     //
  294.     label l0 
  295.         dp3 DotProduct, VertexNormalCameraSpace, -LightToVertexVector
  296.         max DotProduct, DotProduct, Zero 
  297.         mul TempColor.xyz, DotProduct, LightDiffuse    
  298.         mul TempColor.xyz, TempColor, MaterialDiffuse  
  299.         add TempColor.xyz, TempColor, LightAmbient     
  300.         mad OutputDiffuse.xyz, TempColor, AttenuationFactor, OutputDiffuse 
  301.     ret
  302. };
  303.  
  304. PixelShader LightingPS = asm
  305. {
  306.     ps_3_0 
  307.     dcl_color0 v0
  308.     mov oC0, v0
  309. };
  310.  
  311. technique Technique_Lighting_VS_2_0
  312. {
  313.     pass P0
  314.     {
  315.         VertexShader = (Lighting_VS_2_0);
  316.     }
  317. }
  318.  
  319. technique Technique_Lighting_VS_3_0
  320. {
  321.     pass P0
  322.     {
  323.         VertexShader = (Lighting_VS_3_0);
  324.         PixelShader  = (LightingPS);
  325.     }
  326. }
  327.  
  328. technique Technique_Lighting_VS_3_0_No_PS
  329. {
  330.     pass P0
  331.     {
  332.         VertexShader = (Lighting_VS_3_0);
  333.     }
  334. }
  335.